package com.foreverwin.jzymes.production.remedy.controller;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.domain.CarrierBind;
import com.foreverwin.jzymes.common.service.ICarrierBindAdviceService;
import com.foreverwin.jzymes.common.service.ICustomFieldsAdviceService;
import com.foreverwin.jzymes.common.service.IItemAdviceService;
import com.foreverwin.jzymes.common.util.CarrierUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.event.AppEventPublisher;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.remedy.service.IClampCarrierAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/production/remedy/clamp")
public class ClampCarrierController {

    @Autowired
    ICarrierBindAdviceService carrierBindAdviceService;
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;
    @Autowired
    IClampCarrierAdviceService clampCarrierAdviceService;
    @Autowired
    AppEventPublisher appEventPublisher;

    @RequestMapping("/findCarrierBind")
    public AjaxResult findCarrierBind( @RequestParam String carrier ){
        try{
            String site = CommonMethods.getSite();
            Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
            carrier = CarrierUtil.buildCarrier( site, carrier );
            List<CarrierBind> carrierBindList = carrierBindAdviceService.findCarrierBindOnBind( site, carrier );
            List<Map<String,Object>> bindList = new ArrayList<>();
            for( CarrierBind carrierBind : carrierBindList ){
                Map<String,Object> itemMap = itemAdviceService.existItem( carrierBind.getItemBo() );

                Map<String,Object> bindMap = new HashMap<>();
                bindMap.put( "machineType", carrierBind.getMachineType() );
                bindMap.put( "carrier", carrierBind.getCarrier() );
                bindMap.put( "item", itemMap.get( "ITEM" ) );
                bindMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
                bindMap.put( "startDateTime", carrierBind.getStartDateTime() );
                bindList.add( bindMap );
            }
            return AjaxResult.returnSuccess().put( "value", bindList );
        }catch ( Exception e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    @RequestMapping("/carrierBind")
    public AjaxResult carrierBind( @RequestParam String carrier, @RequestParam String resrce ){

        try{
            String site = CommonMethods.getSite();
            Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
            carrier = CarrierUtil.buildCarrier( site, carrier );
            clampCarrierAdviceService.clampCarrier( carrier, resrce );

            /**发布事件*/
            JSONObject eventData = new JSONObject();
            eventData.put( "SITE", site );
            eventData.put( "CARRIER", carrier );
            eventData.put( "RESRCE", resrce );
            AppEvent<JSONObject> appEvent = new AppEvent<>( this );
            appEvent.setName( "CARRIER_CHECK_OUT_EVENT" );
            appEvent.setData( eventData );
            appEventPublisher.publishEvent( appEvent );

            return AjaxResult.returnSuccess();
        }catch ( Exception e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }
}
